package com.kk.app;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.core.content.ContextCompat;

import com.kk.app.utils.TasksHelper;

import java.util.ArrayList;
import java.util.List;

public class PermissionManager {
    private static volatile PermissionManager mInstance;

    public static PermissionManager getInstance() {
        if (mInstance == null) {
            synchronized (PermissionManager.class) {
                if (mInstance == null) {
                    mInstance = new PermissionManager();
                }
            }
        }
        return mInstance;
    }

    public static final int REQUEST_CODE = 10;
    private Activity mActivity;
    private Callback mCallback;
    private final int mRequestCode = 1000;

    public void checkPermission(Activity activity, Callback callback) {
        mActivity = activity;
        mCallback = callback;
        TasksHelper.runWorkTask2(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                requestOverlayPermission();
            }
        });
    }

    /**
     * 释放 activity的引用和回调对象
     */
    public void release() {
        if (mActivity != null) {
            mActivity = null;
        }
        if (mCallback != null) {
            mCallback = null;
        }
    }

    private void requestOverlayPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (!Settings.canDrawOverlays(mActivity)) {
                try {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                            Uri.parse("package:" + mActivity.getPackageName()));
                    mActivity.startActivityForResult(intent, REQUEST_CODE);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                requestPermission();
            }
        } else {
            requestPermission();
        }
    }

    //权限判断和申请
    public void requestPermission() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            if (mCallback != null) mCallback.result(true);
        } else {
            String[] permissions;
            //3399增加相机权限

            permissions = new String[]{Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.MODIFY_AUDIO_SETTINGS,
                    Manifest.permission.INTERNET,
                    Manifest.permission.BLUETOOTH,
                    Manifest.permission.BLUETOOTH_ADMIN,
                    Manifest.permission.RECORD_AUDIO
            };

            List<String> mPermissionList = new ArrayList<>();
            //逐个判断你要的权限是否已经通过
            for (String permission : permissions) {
                if (ContextCompat.checkSelfPermission(mActivity, permission) != PackageManager.PERMISSION_GRANTED) {
                    //添加还未授予的权限
                    mPermissionList.add(permission);
                }
            }
            //申请权限
            if (mPermissionList.size() > 0) {
                //有权限没有通过，需要申请
                mActivity.requestPermissions(permissions, mRequestCode);
            } else {
                //说明权限都已经通过，可以做你想做的事情去
                if (mCallback != null) mCallback.result(true);
            }
        }
    }

    public void setResult(int requestCode, String[] permissions, int[] grantResults) {
        boolean isOK = true;
        switch (requestCode) {
            case mRequestCode: {
                for (int i = 0; i < permissions.length; i++) {
                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
//                        KLog.d("Permissions --> " + "Permission Granted: " + permissions[i]);
                    } else if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
//                        KLog.d("Permissions --> " + "Permission Denied: " + permissions[i]);
                        isOK = false;
                    }
                }
            }
            break;
        }
        if (isOK) {
            if (mCallback != null) mCallback.result(true);
        } else {
            if (mCallback != null) mCallback.result(false);
        }
    }

    public interface Callback {
        public void result(boolean isSuccess);
    }
}
